package com.start;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.game.system.ServerMain;
import com.xpec.c4.game.system.StartInfo;
import com.xpec.c4.service.common.ServiceConfig;
import com.xpec.c4.service.module.ModuleServer;
import com.xpec.c4.service.module.ServerProperties;


/**
 * 這個是要被加密編碼的class<br> 
 * 可以把要啟動的東西寫在這裡, classloader要是沒load到等於會啟動失敗<br>
 * 
 * @author yangbingyan
 *
 */

public class AuthorizeEntry {

	/**
	 * telnet port
	 */
	private static final String TELNET_PORT = "-TelnetPort=";	
	
	/**
	 * 服务器名称
	 */
	private static final String SERVER_NAME = "-ServerName=";

	/**
	 * 服务器配置
	 */
	private static final String SERVER_INFO_CONFIG = "-ServerInfoConfig=";
	
	/**
	 * 
	 */
	private static final Log log = LogFactory.getLog(AuthorizeEntry.class);;
	
	/**
	 * @param args
	 * 
	 * 参数与main() 相同<br>
	 * 把傳給main()的參數也pass到這<br>
	 */
	public void startup(String[] args, StartInfo startInfo) {

		System.out.println("Checking Authorize.");
		
		checkValidDate();

		
		// 默认是全部启动
		String serverName = ModuleServer.ALL_MODULE_START_UP;
		// String serverName = "server03";

		for (String arg : args) {
			if (arg.startsWith(SERVER_INFO_CONFIG)) {
				String value = arg.substring(SERVER_INFO_CONFIG.length());
				ServiceConfig.serverInfoFileName = value;
			} else if (arg.startsWith(SERVER_NAME)) {
				String value = arg.substring(SERVER_NAME.length());
				serverName = value;
			}
			else if(arg.startsWith(TELNET_PORT)){
				String telnetPort = arg.substring(TELNET_PORT.length());
				if(telnetPort != null)
				{
					System.setProperty("com.xpec.c4.telnetPort",telnetPort);
				}
			}
		}
		
		//修改log4j.xml的目录
		//URL url = Utility.getUrl("log4j.xml");
		//log.info(url);
		System.setProperty("log4j.serverName", serverName);
		//DOMConfigurator.configure(url);
		//log.info("ServerInfoConfig:" + ServiceConfig.serverInfoFileName);
		//log.info("ServerName:" + serverName);
		ServerProperties.localServer = serverName;

		ServerMain main = new ServerMain();

		main.setServer(ModuleServer.getInstance());
		 
		try {
			main.init(main);
			main.start();
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e, e);
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	 * @return
	 */
	private void checkValidDate(){	
		
		// 檢查授權時間日期等等
		Calendar currentDate = Calendar.getInstance();
		Calendar expirationDate = Calendar.getInstance();
		
		Date date = new Date();
		System.out.println("Current date: " + date);

		// rightNow.set(2012, 0,1, 3, 12,5);
		
		// Thu Oct 04 15:40:53 CST 2012
		expirationDate.set(2020, 11, 31, 23, 59, 59);
		//expirationDate.set(2016, 11, 31, 23, 59, 59);
		// expirationDate.set(2012, 9, 3, 3, 12,5);
		
		// date.setTime(rightNow.getTimeInMillis());
		date.setTime(expirationDate.getTimeInMillis());
		System.out.println("Expiration date: " + date);
		
		// expiration
		// Authorization has expired		
		
		boolean check;
		if(currentDate.before(expirationDate)){
			check = true;
		}else{
			check = false;
			System.out.println("Authorization has expired.");
		}
		
		if(!check){
			System.exit(0);
			return;
		}
		
	}
	

}
